﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Web.Routing;
namespace Htss.Platform.WebApi
{
    public  class ApiBox
    {
        public ApiBox() 
        {
            
        }
        /// <summary>
        /// 存储指定的NameSpace,BoxName,ActionName
        /// </summary>
        public Dictionary<string,string> ActionBox
        {
            get;
            set;
        }
        public RouteData RouteData
        {
            get { return RequestContext.RouteData; }
        }
        public RequestContext RequestContext
        {
            get;
            set;
        }
        public HttpContextBase HttpContext
        {
            get { return RequestContext.HttpContext; }
        }
        public HttpRequestBase Request
        {
            get { return HttpContext.Request; }
        }

        public HttpResponseBase Response
        {
            get { return HttpContext.Response; }
        }

        public HttpSessionStateBase Session
        {
            get { return HttpContext.Session; }
        }

        public HttpServerUtilityBase Server
        {
            get { return HttpContext.Server; }
        }
        /// <summary>
        /// 初始化默认值
        /// </summary>
        /// <param name="RequestContext"></param>
        public void Init(RequestContext RequestContext,Dictionary<string,string> ActionBox)
        {
            this.RequestContext = RequestContext;
            this.ActionBox = ActionBox;
        }
        /// <summary>
        /// 异常时返回的结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public JsonResult Json(bool resultType,System.Data.DataTable dt) 
        {
            JsonResult json = new JsonResult();
            json.ExecuteResult(HttpContext);
            json.Json(resultType ? "true" : "false", dt);
            return json;
        }

        /// <summary>
        /// 返回正确的结果集
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public JsonResult Json(bool resultType,string resultStr)
        {
            JsonResult json = new JsonResult();
            json.ExecuteResult(HttpContext);
            json.Json(resultType ? "true" : "false", resultStr);
            return json;
        }
    }
}
